package com.example.service.impl;

import com.example.service.UserService;
import com.example.dao.UserRepository;
import com.example.domain.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserRepository userRepository;

    @Override
    public User queryUserById(String id){
        Long l = Long.valueOf(id);
        Optional<User> byId = userRepository.findById(l);
        return byId.isPresent() ? byId.get() : null;
    }
 
    @Override
    public int addUser(User user){
        Optional<User> byId = userRepository.findById(user.getId());
        if (byId.isPresent()) {
            return 0;
        }
        userRepository.save(user);
        return 1;
    }
 
    @Override
    public int dropUser(String id){
        userRepository.deleteById(Long.parseLong(id));
        return  1;
    }
 
    @Override
    public int modifyUser(User user){
        return 1;
    }
 
    @Override
    public List<User> queryAllUser(){
        return userRepository.findAll();
    }
}
